package com.hm.health.records;

import android.content.Context;

import com.goodix.comsdk.entity.MeasureResult;
import com.goodix.comsdk.sdk.OnHBDDataListener;
import com.goodix.libromablecom.sdk.cardiff.arguments.StartArgumentsKeyEnum;
import com.hm.health.MyApplication;
import com.hm.health.entity.BleDevicesInfo;
import com.hm.health.entity.arguments.StartArgumentsManager;
import com.hm.health.records.ecgpatch.EcgPatchRecord;

import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;

/**
 * Created by chenshi on 2020/1/6.
 */

public class TestRecordManager {
    public static final String TAG = "TestRecordManager";

    private ITestRecord mTestRecorder;
    private List<MeasureResult> mMeasureResults = new ArrayList<>();
    private ScheduledExecutorService mExecutorService;
    private Context mContext;
    private AtomicBoolean mIsStop = new AtomicBoolean(false);

    private ITestRecord createTestRecorder(String functionName, Context context) {
        mContext =context;
        ITestRecord testRecord = null;
        switch (functionName) {
            case StartArgumentsKeyEnum.AppFunction_ECG:
                testRecord = new EcgTestRecord();
                break;
        }
        return testRecord;
    }

    public ITestRecord getTestRecord() {
        return mTestRecorder;
    }

    public void start(String startTime, String testerInfo, List<BleDevicesInfo> deviceInfos) {
        mTestRecorder = createTestRecorder(StartArgumentsManager.getInstance().getFunctionName(),
                MyApplication.getContext());
        if (null != mTestRecorder) {
            mTestRecorder.startSaveRecord(startTime, testerInfo, deviceInfos);
        }

        MyApplication.getHBDTestClient().registerHBDDataListener(mOnHBDDataListener);

        mExecutorService = Executors.newSingleThreadScheduledExecutor();
        mExecutorService.scheduleAtFixedRate(new Runnable() {
            @Override
            public void run() {
                if (null != mTestRecorder) {
                    if (mIsStop.get() || mMeasureResults.isEmpty()) {
                        return;
                    }

                    mTestRecorder.onMeasureResult(mMeasureResults);
                }
            }
        }, 0, 1000, TimeUnit.MILLISECONDS);
        mIsStop.set(false);
    }

    public void start(String startTime, String testerInfo, List<BleDevicesInfo> deviceInfos, boolean isEcgFlash) {
        if (isEcgFlash) {
            mTestRecorder = new EcgPatchRecord();
//            testerInfo = testerInfo + "-" + ((int)(Math.random() * 100000)); // TODO: debug
            mTestRecorder.startSaveRecord(startTime, testerInfo, deviceInfos);

            MyApplication.getHBDTestClient().registerHBDDataListener(mOnHBDDataListener);

            mExecutorService = Executors.newSingleThreadScheduledExecutor();
            mExecutorService.scheduleAtFixedRate(new Runnable() {
                @Override
                public void run() {
                    if (null != mTestRecorder) {
                        if (mIsStop.get() || mMeasureResults.isEmpty()) {
                            return;
                        }

                        mTestRecorder.onMeasureResult(mMeasureResults);
                    }
                }
            }, 0, 1000, TimeUnit.MILLISECONDS);
            mIsStop.set(false);
        } else {
            start(startTime, testerInfo, deviceInfos);
        }
    }

    public void stop() {
        mIsStop.set(true);
        if (null != mExecutorService) {
            mExecutorService.shutdownNow();
        }

        if (null != mTestRecorder) {
            mTestRecorder.stopSaveRecord();
        }
    }

    private OnHBDDataListener mOnHBDDataListener = new OnHBDDataListener() {
        @Override
        public void onDataChanged(String dataType, int[] dataArr) {

        }

        @Override
        public void onMeasureResult(MeasureResult result) {
            if (result.mFunctionName.equals(StartArgumentsManager.getInstance().getFunctionName())) {
                int index = mMeasureResults.indexOf(result);
                if (index >= 0) {
                    mMeasureResults.set(index, result);
                } else {
                    mMeasureResults.add(result);
                }
            }
        }
    };

    public static String getRecordFileName(String testerInfo, String startTime) {
        return String.format(Locale.ENGLISH, "%s-%s-%s%s", testerInfo,startTime,
                StartArgumentsManager.getInstance().getFunctionName(),".csv");
    }
}
